The following cells will load the r magics, display documentation on IPython magic and (if you scroll down in the secondary window) R magics. The final cell will show all the availiable magic commands.
In [34]:
# This will load the r magics commands %R (line magic: only the line that follows it is written in R)
# and %%R (cell magic: indicates that the whole cell is written in R.)
# You MUST run this line to make the R magic availiable.
%load_ext rmagic
In [31]:
# use this command to open a window along the bottom of the page
# with a tutorial on using ipython magics.
%magic
In [ ]:
#you can use the ?? notation to get help on a magic function, like so:
%R??
In [32]:
#use this command to see the availiable magic commands
%lsmagic
You can import data using R and work with it in python.
In [26]:
#import the data from R and pass to python interpreter so that you can axcess it with python
sample2 = %R sampledata2 <- read.table("sampledata.txt", header=T, sep=",")
#note the difference between viewing sample2 with print and viewing sample2 as output.
print sample2
sample2
Out[26]:
You can also import data in python and pass it to R with the -i flag , which should be on the same line you invoke the R magics. There is also a -o flag for outputting data from R to python. It should go on the same line as the input flag (-i) and should be followed by the name of the R variable that you want to transfer to the ipython interpreter. You can use these flags with both line magics (%R) and cell magics (%%R).
Single-line R magic:
Python code here
%R -i data -o newdata #R code to convert data to newdata
Python code here can operate on newdata
Whole-cell R magic:
%%R -i data -o newdata
# R code to convert data to newdata
# the next python cell will be able to operate on newdata
There are other flags and arguments availiable:
@argument(
'-i', '--input', action='append',
help='Names of input variable from shell.user_ns to be assigned to R variables of the same names after calling self.pyconverter. Multiple names can be passed separated only by commas with no whitespace.'
)
@argument(
'-o', '--output', action='append',
help="Names of variables to be pushed from rpy2 to shell.user_ns after executing cell body (rpy2's internal facilities will apply ri2ro as appropriate). Multiple names can be passed separated only by commas with no whitespace."
)
@argument(
'-n', '--noreturn',
help='Force the magic to not return anything.',
action='store_true',
default=False
)
@argument_group("Plot", "Arguments to plotting device")
@argument(
'-w', '--width', type=int,
help='Width of plotting device in R.'
)
@argument(
'-h', '--height', type=int,
help='Height of plotting device in R.'
)
@argument(
'-p', '--pointsize', type=int,
help='Pointsize of plotting device in R.'
)
@argument(
'-b', '--bg',
help='Background of plotting device in R.'
)
@argument_group("SVG", "SVG specific arguments")
@argument(
'--noisolation',
help=('Disable SVG isolation in the Notebook. By default, SVGs are isolated to avoid namespace collisions between figures.'
'Disabling SVG isolation allows to reference previous figures or share CSS rules across a set of SVGs.'),
action='store_false',
default=True,
dest='isolate_svgs'
)
@argument_group("PNG", "PNG specific arguments")
@argument(
'-u', '--units', type=unicode, choices=["px", "in", "cm", "mm"],
help='Units of png plotting device sent as an argument to *png* in R. One of ["px", "in", "cm", "mm"].'
)
@argument(
'-r', '--res', type=int,
help='Resolution of png plotting device sent as an argument to *png* in R. Defaults to 72 if *units* is one of ["in", "cm", "mm"].'
)
@argument(
'code',
nargs='*',
)
In [7]:
import pandas as pd
In [8]:
sample = pd.read_excel('sampledata.xlsx')
sample.head()
Out[8]:
In [13]:
# pass in sample from python interpreter,
# get the summary using R,
# print the summary from R interpreter
%R -i sample -o samp_summary samp_summary = summary(sample); print(samp_summary);
In [11]:
#print the passed-back summary from python interpreter
print samp_summary
In [ ]:
or like this...
In [14]:
#my computer refuses to complete this line, maybe yours won't.
%R -i sample View(sample$Sex)
In [15]:
%%R -i sample
weight <- sample$Weight
hist(weight)
In [42]:
%%R
hist(weight, breaks=10)
In [34]:
%%R -i sample
age = sample$Age
plot(weight, age)
In [43]:
%%R
plot(weight, age, xlab="Weight", ylab="Age", main="Weight vs Age", las=1)